<template>
  <div class="image-container">
    <img v-if="!everythingDone" class="placeholder" :src="placeholder" />
    <img
      @load="handleload"
      :src="src"
      :style="{ opacity: originOpacity, transition: `${duration}ms` }"
    />
  </div>
</template>

<script>
export default {
  props: {
    src: {
      type: String,
      required: true,
    },
    placeholder: {
      type: String,
      required: true,
    },
    duration: {
      type: Number,
      default: 500,
    },
  },
  data() {
    return {
      originLoaded: false,
      everythingDone: false,
    };
  },
  computed: {
    originOpacity() {
      return this.originLoaded ? 1 : 0;
    },
  },
  methods: {
    handleload() {
      // setTimeout(() =>{
      this.originLoaded = true;
      this.everythingDone = true;
      this.$emit("load");
      // },this.duration)
    },
  },
};
</script>

<style scoped lang="less">
@import "~@/style/mixin.less";
.image-container {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    .self-fill();
  }
  .placeholder {
    filter: blur(2vw);
  }
}
</style>